package cn.biplam.front.template.control.rfFlowTable;

import java.util.ArrayList;

import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.treeFlat.flat.callback.CompareCallback;
import cn.biplam.front.callback.CallBack;
import cn.biplam.front.template.control.rfFlowTable.callback.action.ActionCallback;
import cn.biplam.front.template.control.rfFlowTable.callback.content.BaseContentCallback;
import cn.biplam.front.template.control.rfFlowTable.callback.content.ContentCallback;
import cn.biplam.front.template.control.rfFlowTable.callback.content.IndexColCallback;

/**
 * 表格列属性存放器<BR>
 * 单独为ReFlowTable的创建服务<BR>
 * 功能:将表格接口类中的设置的列信息存储,提供给表格绘制类访问<BR>
 * @author XiaoRuxing Nov 6, 200911:59:10 AM
 * @version 1.0
 */
public class RfFlowTablePropertys {

//	/***********************
//	* 绘制表格列所需信息
//	***********************/
	/** 每列对应数据集的列名 */
	protected ArrayList<String>  colFieldName;
	/** 每列对应标题 */
	private KvMap<String, String> colTitle;
	/** 列内容的回调器 */
	private KvMap<String, CallBack> colCallback;
	/** 字段动作回调类*/
	private KvMap<String, ActionCallback> filedActCallbacks;
	/** 字段动作绑定判断回调类*/
	private KvMap<String, CompareCallback> fieldCompareCallbacks;
	/** 每列宽度 */
	private KvMap<String, Integer> colWidth;

	/** 表格行索引列列名 */
	private final String INDEX_COL_NAME = ".flowTableIncexColName";
	/** 是否有标题 */
	private boolean isHaveTitle = false;

//	******************************
//	存储操作按钮信息的容器
//	******************************
	private ArrayList<String> buttons;
	private KvMap<String, ActionCallback> buttonopActCallbacks;
	private KvMap<String, CompareCallback> buttonCompareCallbacks;
	private KvMap<String, ContentCallback> buttonContentCallbacks;
	private String operatorColTitle = "操作";//操作列名称

	/** 表格列数 */
	private int colCount = -1;
	/** 行按钮个数 */
	private int colBtCount = -1;

	public RfFlowTablePropertys(){
		this.colFieldName = new ArrayList<String>();
		this.colTitle = new KvMap<String, String>();
		this.colCallback = new KvMap<String, CallBack>();
		this.fieldCompareCallbacks = new KvMap<String, CompareCallback>();
		this.filedActCallbacks = new KvMap<String, ActionCallback>();
		this.colWidth = new KvMap<String, Integer>();

		this.buttons = new ArrayList<String>();
		this.buttonopActCallbacks = new KvMap<String, ActionCallback>();
		this.buttonCompareCallbacks = new KvMap<String, CompareCallback>();
		this.buttonContentCallbacks = new KvMap<String, ContentCallback>();
	}

	/**
	 * 放入一个列字段,指定标题文字、没有动作
	 * @param fieldName		列字段名
	 * @param sFieldCaption		列标题文字
	 */
	protected void putCol(String fieldName, String fieldCaption) throws ErrorMessage{
		this.putCol(fieldName, fieldCaption, new BaseContentCallback(fieldName));
	}

	/**
	 * 放入一个列字段,指定标题文字、没有动作
	 * @param fieldName		列字段名
	 * @param fieldCaption		列标题文字
	 * @param contentCallback   列内容回调器
	 * @throws ErrorMessage 
	 */
	protected void putCol(String fieldName, String fieldCaption, CallBack contentCallback) throws ErrorMessage {
		this.putCol(fieldName, fieldCaption, contentCallback, null);
	}

	/**
	 * 放入一个列字段,由Callback创建标题文字,由Callback指定动作
	 * @param fieldName			列字段名		
	 * @param fieldCaption		列标题文字
	 * @param contentCallback	列内容回调类
	 * @param actCallback		动作回调类
	 * @throws ErrorMessage 
	 */
	protected void putCol(String fieldName, String fieldCaption, CallBack contentCallback,
			ActionCallback actCallback) throws ErrorMessage {
		this.putCol(fieldName, fieldCaption, 
				contentCallback, actCallback, null);
	}

	/**
	 * * 放入一个列字段,由Callback创建标题文字,判断是否绑定动作,由Callback指定动作
	 * @param fieldName			列字段名		
	 * @param fieldCaption		列标题文字
	 * @param contentCallback	列内容回调类
	 * @param actCallback		动作回调类
	 * @param compareCallback   列内容比较器(根据比较规则生成列内容)
	 * @throws ErrorMessage 
	 */
	protected void putCol(String fieldName, String fieldCaption, CallBack contentCallback,
			ActionCallback actCallback, CompareCallback compareCallback) throws ErrorMessage {
		this._setColFieldName(fieldName);
		this._setColTitle(fieldName, fieldCaption);
		this._setColCallback(fieldName, contentCallback);
		this._setColActionCallback(fieldName, actCallback);
		this._setColCompareCallBack(fieldName, compareCallback);
	}

	/**
	 * @return 设置显示索引列
	 * @throws ErrorMessage 
	 */
	protected void setShowIndexCol(boolean showIndexCol) throws ErrorMessage{
		this.putIndexCol();
	}

	/**
	 *  放入操作按钮
	 * @param buttonCaption 按钮标题
	 * @param actCallback   按钮动作回调器
	 * @throws ErrorMessage 
	 */
	protected void putButton(String buttonCaption,
			ActionCallback actCallback) throws ErrorMessage{
		this.putButton(buttonCaption, actCallback,null);
	}

	/**
	 *  放入操作按钮
	 * @param buttonCaption   按钮标题
	 * @param actCallback     动作回调器
	 * @param compareCallback 列内容比较回调器
	 * @throws ErrorMessage 
	 */
	protected void putButton(String buttonCaption,
			ActionCallback actCallback,CompareCallback compareCallback) throws ErrorMessage{
		this.putButton(buttonCaption, actCallback, compareCallback,null);
	}

	/**
	 *  放入操作按钮
	 * @param buttonCaption   按钮标题
	 * @param actCallback     动作回调器
	 * @param compareCallback 按钮是否显示比较回调器
	 * @param contentCallback 按钮标题回调器
	 * @throws ErrorMessage 
	 */
	protected void putButton(String buttonCaption,
			ActionCallback actCallback,
			CompareCallback compareCallback,
			ContentCallback contentCallback) throws ErrorMessage{
		this.buttons.add(buttonCaption);
		this.buttonopActCallbacks.put(buttonCaption, actCallback);
		if(compareCallback!=null){
			this.buttonCompareCallbacks.put(buttonCaption, compareCallback);
		}
		if(contentCallback!=null){
			this.buttonContentCallbacks.put(buttonCaption, contentCallback);
		}
	}

	/**
	 *  设置指定列宽度(英文字符数)
	 * @param sColFieldName  列字段名
	 * @param iColSize       列宽度(字符数)
	 * @throws ErrorMessage 
	 */
	protected void setColSize(String sColFieldName,int iColSize) throws ErrorMessage{
		this._setColWidth(sColFieldName, TOOLS.getPxLength(iColSize));
	}

	/**
	 *  设置列宽度(Px)
	 * @param sColFieldName 列字段名
	 * @param width         列宽度(Px)
	 * @throws ErrorMessage 
	 */
	protected void setColWidth(String sColFieldName, String width) throws ErrorMessage{
		this._setColWidth(sColFieldName, TOOLS.stringToInt(width));
	}

	/**
	 *  操作列标题
	 * @param sTitleName 操作列标题
	 */
	protected void setOperatorColTitle(String sTitleName){
		this.operatorColTitle = sTitleName;
	}

	/**
	 * @return 表格列数
	 */
	protected int getColCount(){
		if(this.colCount<0)
			this.colCount = this.colFieldName.size();
		return this.colCount;
	}

	/**
	 * @return 单元格排序号
	 */
	protected int getColNumb(String fieldName){
		return this.colFieldName.indexOf(fieldName);
	}

	/**
	 *  获取列名
	 * @param colNum 列序号
	 * @return 列名
	 */
	protected String getFieldName(int colNum){
		return this.colFieldName.get(colNum);
	}


	/**
	 * @return 获取取操作列标题
	 */
	public String getOperatorColTitle() {
		return operatorColTitle;
	}

	/**
	 *  获取列标题
	 * @param fieldName 列名称
	 * @return 列标题
	 */
	protected String getFieldCaption(String fieldName){
		return this.colTitle.getByKey(fieldName);
	}

	/**
	 *  获取列内容回调器
	 * @param fieldName 列名
	 * @return 列内容回调器
	 */
	protected CallBack getFieldCotentCallback(String fieldName){
		return this.colCallback.getByKey(fieldName);
	}

	/**
	 * @param fieldName 列名
	 * @return 列动作回调器
	 */
	protected ActionCallback getFieldActionCallback(String fieldName){
		return this.filedActCallbacks.getByKey(fieldName);
	}

	/**
	 * @param fieldName 列名
	 * @return          列内容比较回调器
	 */
	protected CompareCallback getFieldCompareCallback(String fieldName){
		return this.fieldCompareCallbacks.getByKey(fieldName);
	}

	/**
	 * @param fieldName 列名
	 * @return 获取列宽度
	 *         当前列没设置宽度时返回-1
	 */
	protected int getColWidth(String fieldName){
		Integer tr =  this.colWidth.getByKey(fieldName);
		return tr==null?-1:tr;
	}

	/**
	 * @return 每行按钮个数
	 */
	protected int getBtCount(){
		if(this.colBtCount<0)
			this.colBtCount = this.buttons.size();
		return this.colBtCount;
	}

	/**
	 *  获取按钮标题
	 * @param btNumber 按钮排序号
	 * @return 按钮标题
	 */
	protected String getBtCaption(int btIndex){
		return this.buttons.get(btIndex);
	}

	/**
	 * @param btCaption 按钮标题
	 * @return 按钮动作回调器
	 */
	protected ActionCallback getBtActCallback(String btCaption){
		return this.buttonopActCallbacks.getByKey(btCaption);
	}

	/**
	 * @param btCaption 按钮标题
	 * @return 按钮是否有效比较回调器
	 */
	protected CompareCallback getBtCompareCallback(String btCaption){
		return this.buttonCompareCallbacks.getByKey(btCaption);
	}

	/**
	 * @param btCaption 按钮标题
	 * @return 按钮标题回调器
	 */
	protected ContentCallback getBtContentCallback(String btCaption){
		return this.buttonContentCallbacks.getByKey(btCaption);
	}

	/**
	 *  
	 * @return 是否有标题行
	 */
	public boolean isHaveTitle(){
		return this.isHaveTitle;
	}

	/**
	 * 设置行宽度
	 * @throws ErrorMessage 
	 */
	private void _setColWidth(String colName, int size) throws ErrorMessage{
		this.colWidth.put(colName, size);
	}

	/**
	 * 设置行对应字段名（不允许重复）
	 * @param colName 字段名
	 * @throws ErrorMessage
	 */
	private void _setColFieldName(String colName) throws ErrorMessage{
		this.colFieldName.add(colName);
	}

	/**
	 *  设置列标题
	 * @param colName
	 * @param colTilte
	 * @throws ErrorMessage 
	 */
	private void _setColTitle(String colName,String fieldCaption) throws ErrorMessage{
		this.colTitle.put(colName, fieldCaption);
		if(fieldCaption!=null || "".equals(fieldCaption)){
			this.isHaveTitle = true;
		}
	}

	/**
	 * 设置列内容回调器
	 */
	private void _setColCallback(String colName,CallBack callback) throws ErrorMessage{
		if(callback!=null)
			this.colCallback.put(colName, callback);
	}

	/**
	 * 设置列动作回调器
	 */
	private void _setColActionCallback(String colName,ActionCallback actCallback) throws ErrorMessage{
		if(actCallback!=null)
			this.filedActCallbacks.put(colName, actCallback);
	}

	/**
	 *  设置列比较回调器
	 */
	private void _setColCompareCallBack(String colName,CompareCallback compareCallback) throws ErrorMessage{
		if(compareCallback!=null)
			this.fieldCompareCallbacks.put(colName, compareCallback);
	}

	/**
	 *  插入索引列
	 */
	private void putIndexCol() throws ErrorMessage{
		this.colFieldName.add(0, this.INDEX_COL_NAME);
		this.colTitle.put(this.INDEX_COL_NAME,"  ");
		this._setColCallback(this.INDEX_COL_NAME, new IndexColCallback());
	}
}
